// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	2/12/2015 10:14:41 PM				
// -----------------------------

#include "PlatformProvider.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <tchar.h>

namespace Flow
{

	struct PlatformData
	{
		HMODULE	Library;
	};

	PlatformData* PlatformProvider::s_lpPlatformData = nullptr;

	Bool PlatformProvider::LoadPlatformLibrary()
	{
		s_lpPlatformData = new PlatformData();

		#ifdef ARM
		TChar* lpLibName = _T("FlowWP8.dll");
		#elif defined _WIN64
		TChar* lpLibName = _T("Flow64.dll");
		#else
		TChar* lpLibName = _T("Flow32.dll");
		#endif

		#ifdef ARM
		s_lpPlatformData->Library = LoadPackagedLibrary(lpLibName, 0);
		#else
		s_lpPlatformData->Library = LoadLibrary(lpLibName);
		#endif

		if (s_lpPlatformData->Library == NULL)
		{
			return false;
		}

		return true; 
	}
	void PlatformProvider::UnloadPlatformLibrary()
	{
		if (s_lpPlatformData->Library == NULL)
			return;

		FreeLibrary(s_lpPlatformData->Library);

		delete s_lpPlatformData;
		s_lpPlatformData = nullptr;
	}

	void* PlatformProvider::RequestFunction(Char* lpObjectIdentifier)
	{
		const Size_T bufferLen = 64;
		Char buffer[bufferLen];
		memset(buffer, 0, sizeof(Char)* bufferLen);
		
		strcat_s(buffer, bufferLen, "__Create");
		strcat_s(buffer, bufferLen, lpObjectIdentifier);

		return GetProcAddress(s_lpPlatformData->Library, buffer);
	}

}